home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 015 / hpstuff.arc / HPXFER.C < prev    next >
Text File  |  1987-02-11  |  4KB  |  139 lines

  1.  
  2. /*======================================================================*\
  3. || HPXFER                          Rip Toren     ||
  4. ||                              POB 674        ||
  5. ||                              Columbia, MD 21045||
  6. ||                                    ||
  7. ||  Will transfer a file of HP Laserjet commands down the LPT line    ||
  8. ||  to the printer.                            ||
  9. ||  A line beginning with '@' is a file name of a binary image to       ||
  10. ||  be sent as is.                            ||
  11. ||                                    ||
  12. ||  all chacacters after '~' will not be transfered.                    ||
  13. ||  arg1 is the file name                        ||
  14. ||                                    ||
  15. \*======================================================================*/
  16.  
  17. #include "stdio.h"
  18. #include "stdlib.h"
  19.  
  20. char inline[256] , *p, *q, *r;
  21.  
  22. int  ret,lc;
  23.  
  24. FILE  *inf, *prt, *incl;
  25.  
  26. main (argc,argv)
  27.  
  28. int argc;
  29. char *argv[];
  30.    {
  31.  
  32.    puts ("HP LaserJet XFER program\n");
  33.    if (argc == 1 )
  34.        {
  35.        puts ("  No input filename supplied !");
  36.        puts (" ");
  37.        puts ("  Standard form is 'HPXFER fn'");
  38.        puts ("     where 'fn' is the file name to transfer");
  39.        puts ("     if the file is not found, the extension");
  40.        puts ("     of '.HPX' is appended and tried again.");
  41.        exit (1);
  42.        }
  43.  
  44.  
  45.    strcpy (inline,argv[1]);           /* get the file name            */
  46.  
  47.    if ((inf = fopen (inline,"rb")) == NULL)
  48.        {                   /* file not found            */
  49.        strcat (inline,".HPX");
  50.        if ((inf = fopen (inline,"rb")) == NULL)
  51.        {                   /* that file not found also        */
  52.        puts (" ");
  53.        puts ("FILE NOT FOUND!");
  54.        exit(1);
  55.        }
  56.        }
  57.    printf ("File being processed is %s\n",inline);
  58.  
  59.    /*-------------------------------------------------------------------*\
  60.    |   file is now open                          |
  61.    \*-------------------------------------------------------------------*/
  62.    /*-------------------------------------------------------------------*\
  63.    |   Now open the printer                         |
  64.    \*-------------------------------------------------------------------*/
  65.    prt = fopen ("PRN", "wb");
  66.    if (prt == NULL)
  67.        {
  68.        puts ("Printer failed to open");
  69.        exit (1);
  70.        }
  71.    setnbf (prt);
  72.  
  73.  
  74.    puts ("here we go!");
  75.    lc = 0;
  76.    for (;;)
  77.        {
  78.        memset (inline,'\0',255);
  79.        r = fgets (inline,255,inf);
  80.  
  81.        if (ret=ferror(inf))
  82.        {
  83.        printf ("FERROR %d\n",ret);
  84.        }
  85.  
  86.        if (feof(inf))
  87.        {
  88.        puts ("\nEOF");
  89.        fclose(inf);
  90.        fclose(prt);
  91.        exit (0);
  92.        }
  93.  
  94.        printf ("%4d%c%c%c%c",++lc,0x08,0x08,0x08,0x08);
  95.        p = inline;               /* set the step pointer            */
  96.        for (;*p;p++) if (*p == '~') *p = '\0';
  97.        p = inline;
  98.        if (*p == '@')  print_incl(p);
  99.      else
  100.                        /* print the character            */
  101.       for ( ; *p ; p++)  fprintf (prt,"%c",*p);
  102.        }
  103.    }
  104.  
  105.  
  106. /*===========================================================================*\
  107. || Transfer the binary image .                             ||
  108. \*===========================================================================*/
  109. #define BUF_S 32000
  110. print_incl (p)
  111. char *p;
  112.    {
  113.    char *bf ;
  114.    int i,eol;
  115.  
  116.    eol = strlen (p);
  117.    if ((eol) && (p[eol-2] == 0x0d))    /* some characters in here         */
  118.                        /* and a CRLF                 */
  119.      {
  120.      *(p+eol-2) = '\0';   /* overlay 0x0d 0x0a with terminator */
  121.      }
  122.    printf ("\nIncluding [%s]\n",p+1);
  123.    incl = fopen(p+1,"rb");
  124.    if (incl == NULL) return;
  125.    bf = malloc (BUF_S);
  126.    if (bf == NULL)
  127.        {
  128.        printf ("No space for file buffer \n");
  129.        return;
  130.        }
  131.    for (;;)
  132.        {
  133.        i = fread (bf,1,BUF_S,incl);
  134.        fwrite     (bf,1,i,prt);
  135.        if (i != BUF_S) break;
  136.        }
  137.    fclose (incl);
  138.    }
  139.